package com.example.sh_gameox;

import android.os.Vibrator;
import android.widget.ImageView;

import static android.content.Context.VIBRATOR_SERVICE;
import static com.example.sh_gameox.Application.getContext;

public class PublicFunction {
    private static Vibrator vibrator = (Vibrator) getContext().getSystemService(VIBRATOR_SERVICE);

    public static int[][] getGame1(int id){
        int[][] ret = new int[6][6];
        for(int i=0;i<ret.length;i++){
            for(int j=0;j>ret[0].length;j++){
                ret[i][j] = 0;
            }
        }
        switch (id){
            case 1:
                ret[0][2] = 1;
                ret[1][2] = 1;
                ret[2][0] = 1;
                ret[2][5] = 1;
                ret[3][2] = 2;
                ret[4][1] = 1;
                ret[4][5] = 1;
                ret[5][0] = 2;
                ret[5][4] = 2;
                break;
            case 2:
                ret[0][1] = 1;
                ret[0][4] = 2;
                ret[0][5] = 2;
                ret[1][1] = 1;
                ret[2][3] = 2;
                ret[4][1] = 1;
                ret[4][4] = 1;
                ret[4][5] = 1;
                ret[5][0] = 1;
                ret[5][5] = 1;
                break;
            case 3:
                ret[0][0] = 1;
                ret[0][5] = 1;
                ret[2][0] = 2;
                ret[2][2] = 1;
                ret[2][3] = 1;
                ret[3][2] = 2;
                ret[3][3] = 2;
                ret[3][5] = 1;
                ret[5][0] = 2;
                ret[5][3] = 1;
                ret[5][5] = 2;
                break;
            case 4:
                ret[0][2] = 2;
                ret[1][1] = 1;
                ret[1][4] = 1;
                ret[2][2] = 1;
                ret[3][0] = 2;
                ret[3][3] = 2;
                ret[3][3] = 2;
                ret[3][5] = 2;
                ret[4][1] = 1;
                ret[4][4] = 1;
                ret[5][3] = 2;
                break;
        }
        return ret;
    }
    public static int[][] getGame2(int id){
        int[][] ret = new int[8][8];
        for(int i=0;i<ret.length;i++){
            for(int j=0;j>ret[0].length;j++){
                ret[i][j] = 0;
            }
        }
        switch (id){
            case 5:
                ret[0][3] = 1;
                ret[0][5] = 1;
                ret[1][1] = 1;
                ret[2][4] = 1;
                ret[2][5] = 1;
                ret[2][7] = 1;
                ret[3][2] = 2;
                ret[3][6] = 2;
                ret[4][1] = 1;
                ret[4][4] = 1;
                ret[5][3] = 2;
                ret[5][6] = 1;
                ret[5][7] = 1;
                ret[6][1] = 1;
                ret[7][2] = 2;
                ret[7][6] = 2;
                break;
            case 6:
                ret[0][1] = 2;
                ret[0][2] = 1;
                ret[0][4] = 1;
                ret[0][6] = 1;
                ret[1][0] = 1;
                ret[1][3] = 2;
                ret[1][6] = 1;
                ret[3][1] = 1;
                ret[3][4] = 2;
                ret[3][5] = 2;
                ret[4][1] = 2;
                ret[4][7] = 1;
                ret[5][0] = 2;
                ret[5][5] = 1;
                ret[6][2] = 1;
                ret[6][7] = 1;
                ret[7][0] = 2;
                break;
        }
        return ret;
    }
    public static boolean checkResult(int[][] result){
        boolean ret = true;
        //判断是否全有内容
        for(int i=0;i<result.length;i++){
            for(int j=0;j<result[i].length;j++){
                if(result[i][j]==0){
                    return false;
                }
            }
        }
        //判断是否有三个相连的相同的数据
        for(int i=0;i<result.length;i++){
            for(int j=0;j<result[i].length;j++){
                if(!checkItemOk(result,i,j)){
                    return false;
                }
            }
        }
        //判断每行和每列之和是否相同
        int[] count_col = new int[result.length];
        int[] count_row = new int[result.length];
        for(int i=0;i<result.length;i++){
            for(int j =0;j<result.length;j++){
                if(result[i][j]==1){
                    count_row[i]++;
                    count_col[j]++;
                }
            }
        }
        for(int i=0;i<count_row.length;i++){
            if(count_row[i]!=result.length/2){
                return false;
            }
            if(count_col[i]!=result.length/2){
                return false;
            }
        }
        return ret;
    }

    /**
     * 判断某个格的数据是否正确，检测时从0，0开始检测，所以只需要向右下检测是否正确，不需要再向上检测结果是否正确
     * @param result
     * @param i1
     * @param j1
     * @return
     */
    private static boolean checkItemOk(int[][] result, int i1, int j1){
        boolean ret = true;
        if(i1<result.length-2){
            int i2 = i1+1;
            int i3 = i1+2;
            if(result[i1][j1]==result[i2][j1]&&result[i1][j1]==result[i3][j1]){
                return false;
            }
        }
        if(j1<result.length-2){
            int j2 = j1+1;
            int j3 = j1+2;
            if(result[i1][j1]==result[i1][j2]&&result[i1][j1]==result[i1][j3]){
                return false;
            }
        }
        return ret;
    }
    /**
     * 震动200毫秒
     */
    public static void vibrator(){
        vibrator.vibrate(200);
    }
}
